/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.module.background.controller;

import com.github.lvyanyang.annotation.Authorize;
import com.github.lvyanyang.core.*;
import com.xci.edu.core.entity.EduQuestion;
import com.xci.edu.core.entity.ExamPaper;
import com.xci.edu.core.filter.EduQuestionFilter;
import com.xci.edu.core.filter.ExamPaperFilter;
import com.xci.edu.core.service.EduQuestionService;
import com.xci.edu.core.service.ExamPaperService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 试卷信息页面控制器
 * @author 马星俊 maxingjun@xci96716.com
 * @since 2020-11-25 10:55:25
 */
@Authorize
@Controller
@RequestMapping(value = "/background/examPaper")
public class ExamPaperController extends BaseWebController {
    /** 试卷信息服务 */
    @Resource private ExamPaperService examPaperService;
    /** 试题库服务 */
    @Resource private EduQuestionService eduQuestionService;
    //region 页面

    /** 首页 */
    @GetMapping
    @Authorize(code = "background.examPaper")
    public String index() {
        return "background/examPaper/index";
    }

    /** 新建页 */
    @GetMapping("/create")
    @Authorize(code = "background.examPaper.insert")
    public String create(ModelMap map) {
        createMark(map);
        var entity = new ExamPaper();
        entity.setId(XCI.nextId());
        map.put("entity", entity);
        return "background/examPaper/edit";
    }

    /** 添加试题页 */
    @GetMapping("/createDetails")
    @Authorize(code = "background.examPaper.insert")
    public String createDetails(String id, ModelMap map) {
        map.put("paperId", id);
        return "background/examPaper/paper";
    }
    /** 添加试题 */
    @ResponseBody
    @PostMapping("/saveQuestions")
    @Authorize(code = "background.examPaper.insert")
    public BoolResult saveQuestions(String paperId, String[] ids) {
        return examPaperService.batchSave(paperId,ids);
    }

    /** 编辑页 */
    @GetMapping("/edit")
    @Authorize(code = "background.examPaper.update")
    public String edit(String id, ModelMap map) {
        var entity = examPaperService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/examPaper/edit";
    }

    /** 详情页 */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = examPaperService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/examPaper/details";
    }

    //endregion

    //region 处理

    /** 表格查询 */
    @ResponseBody
    @PostMapping("/grid")
    @Authorize(code = "background.examPaper.select")
    public BoolResult grid(ExamPaperFilter filter) {
        return BoolResult.success(examPaperService.selectPageList(filter));
    }

    /** 新建保存 */
    @ResponseBody
    @PostMapping("/createSave")
    @Authorize(code = "background.examPaper.insert")
    public BoolResult createSave(@ModelAttribute ExamPaper entity) {
        return examPaperService.insert(entity);
    }

    /** 修改保存 */
    @ResponseBody
    @PostMapping("/updateSave")
    @Authorize(code = "background.examPaper.update")
    public BoolResult updateSave(@ModelAttribute ExamPaper entity) {
        return examPaperService.update(entity);
    }

    /** 删除 */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "background.examPaper.delete")
    public BoolResult delete(String ids) {
        return examPaperService.delete(ids);
    }

    /** 修改状态 */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "background.examPaper.updateStatus")
    public BoolResult status(String id, Integer status) {
        examPaperService.updateStatus(id, XCI.toBool(status));
        return BoolResult.success();
    }

    /** 导出 */
    @GetMapping("/export")
    @Authorize(code = "background.examPaper.export")
    public void export(ExamPaperFilter filter) {
        XCI.exportExcel(examPaperService.selectList(filter), ExamPaper.class, "试卷信息列表");
    }

    //endregion
}